package com.xiaoming.service;

import com.aliyun.oss.OSSClient;
import com.aliyun.oss.model.OSSObject;
import com.xiaoming.bean.Result;
import com.xiaoming.config.AliyunConfig;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.web.multipart.MultipartFile;

import javax.servlet.ServletOutputStream;
import javax.servlet.ServletResponse;
import java.io.*;
import java.util.Arrays;
import java.util.List;
import java.util.UUID;

@Service
public class OOSService {
    @Autowired
    private AliyunConfig aliyunConfig;
    @Autowired
    private OSSClient ossClient;
    // 允许上传的格式
    private static final List<String> IMAGE_TYPE = Arrays.asList(new String[]{".jpg", ".jpeg", ".png"});

    private static final int MAX_POST_SIZE = 5 * 1024 * 1024;

    public Result upload(MultipartFile file) {
        String fileName = file.getOriginalFilename();
        Result result = new Result();
        String suffix = fileName.substring(fileName.lastIndexOf("."));
        if(file.getSize() > MAX_POST_SIZE){
            result.setCode(Result.CODE.FAILED);
            result.setMessage("文件大小不能超过 5M");
            return result;
        }
        if (!IMAGE_TYPE.contains(suffix)) {
            result.setCode(Result.CODE.FAILED);
            result.setMessage("文件只支持 png/jpg/jpeg");
            return result;
        }
        try {
            fileName = fileName.substring(0,fileName.lastIndexOf(".")) + UUID.randomUUID().toString() + suffix;
            ossClient.putObject(aliyunConfig.getBucketName(), fileName, new ByteArrayInputStream(file.getBytes()));
        } catch (IOException e) {
            e.printStackTrace();
            result.setCode(Result.CODE.FAILED);
            result.setMessage("文件上传失败");
            return result;
        }
        result.setCode(Result.CODE.SUCCESS);
        result.setMessage("上传成功");
        result.setData(fileName);
        return result;
    }

    public Result download(String fileName, ServletResponse response) {
        try {
            OSSObject object = ossClient.getObject(aliyunConfig.getBucketName(), fileName);
            InputStream inputStream = object.getObjectContent();
            byte[] bytes = new byte[1024];
            int byteread = 0;
            ServletOutputStream outputStream = response.getOutputStream();
            while ((byteread = inputStream.read(bytes)) != -1) {
                outputStream.write(bytes, 0, byteread);
            }
            outputStream.flush();
            outputStream.close();
        } catch (Exception e) {
            e.printStackTrace();
            Result result = new Result();
            result.setCode(Result.CODE.SUCCESS);
            result.setMessage("上传成功");
            result.setData(fileName);
            return result;
        }
        return null;
    }

    public Result delete(String fileName) {
        ossClient.deleteObject(aliyunConfig.getBucketName(), fileName);
        Result result = new Result();
        result.setCode(Result.CODE.SUCCESS);
        result.setMessage("删除成功");
        result.setData(fileName);
        return result;
    }
}
